<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/longest-palindromic-substring/
      思路：https://leetcode-cn.com/problems/longest-palindromic-substring/solution/hui-wen-wen-ti-dong-tai-gui-hua-jspython5-zui-chan/
    */
    let s = "bababba";

    function longestPalindrome(s) {
      if (s.length === 0) return '';
      let res = '';
      let dp = [];

      for (let i = s.length - 1; i >= 0; i--) {
        dp[i] = [];
        for (let j = i; j < s.length; j++) {
          if (i === j) dp[i][j] === true;
          else if (j - i === 1 && s[i] === s[j]) dp[i][j] === true;
          else if (s[i] === s[j] && dp[i+1][j-1]) dp[i][j] === true;

          if (dp[i][j] && j - i + 1 > res.length)
            res = s.slice(i, j + 1);
        }
      }
      return res;
    }
    console.log(longestPalindrome(s));
  </script>
</body>
</html>